package team.seekers.oj.lesson.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import team.seekers.oj.auth.enums.PermissionEnum;
import team.seekers.oj.common.entity.UserSessionDTO;
import team.seekers.oj.lesson.conventer.ChapterConverter;
import team.seekers.oj.lesson.conventer.ChapterManageConverter;
import team.seekers.oj.lesson.dao.ChapterDao;
import team.seekers.oj.lesson.dto.ChapterCreateReqDTO;
import team.seekers.oj.lesson.dto.ChapterManageDTO;
import team.seekers.oj.lesson.entity.ChapterDO;

@Slf4j
@Service
public class ChapterManageService {

    @Autowired
    private ChapterDao chapterDao;

    @Autowired
    private ChapterConverter chapterConverter;

    @Autowired
    private ChapterManageConverter chapterManageConverter;

    public Long create(ChapterCreateReqDTO chapterCreateReqDTO){
        ChapterDO chapterDO = chapterConverter.from(chapterCreateReqDTO);
        chapterDao.save(chapterDO);
        return chapterDO.getChapterId();
    }

    public void update(ChapterManageDTO chapterUpdateReqDTO){
        ChapterDO chapterDO = chapterManageConverter.from(chapterUpdateReqDTO);
        chapterDao.updateById(chapterDO);
    }

    public void delete(Long chapterId){
        ChapterManageDTO query = query(chapterId);
        // TODO 删除所有的section
        chapterDao.removeById(chapterId);
    }

    public ChapterManageDTO query(Long chapterId){
        ChapterDO chapterDO = chapterDao.getById(chapterId);
        ChapterManageDTO chapterManageDTO = chapterManageConverter.to(chapterDO);
        return chapterManageDTO;
    }

    public boolean isChapterManager(Long chapterCreatorId, UserSessionDTO userSessionDTO) {
        return PermissionEnum.SUPERADMIN.in(userSessionDTO) ||
                userSessionDTO.userIdEquals(chapterCreatorId);
    }



}
